﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using weikebuluo.DBUtility;

namespace weikebuluo.DAL
{
    /// <summary>
    /// 数据访问类:w_reply_dal
    /// </summary>
    public partial class w_reply_dal
    {
        DataCreator<weikebuluo.Model.w_reply_model> CreateSQL = new DataCreator<weikebuluo.Model.w_reply_model>();

        public w_reply_dal()
        { }

        #region 增加一条数据
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(weikebuluo.Model.w_reply_model model)
        {
            SqlParameter[] parameters = CreateSQL.CreateParameter(model);

            string strSql = CreateSQL.CreateInsertSql(model, "w_reply", "reply_id") + ";select @@IDENTITY";

            object obj = DbHelperSQL.GetSingle(strSql, parameters);

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #endregion 增加一条数据

        #region 更新一条数据
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(weikebuluo.Model.w_reply_model model)
        {
            SqlParameter[] parameters = CreateSQL.CreateParameter(model);

            string strSql = CreateSQL.CreateUpdateSql(model, "w_reply", "reply_id", "reply_id=" + model.reply_id);

            int rows = DbHelperSQL.ExecuteSql(strSql, parameters);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion 更新一条数据

        #region 删除一条数据
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int reply_id)
        {
            string strSql = CreateSQL.CreateDeleteSql("w_reply", "reply_id=" + reply_id);

            int rows = DbHelperSQL.ExecuteSql(strSql);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion 删除一条数据

        #region 得到一个对象实体
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public weikebuluo.Model.w_reply_model GetModel(int reply_id)
        {
            weikebuluo.Model.w_reply_model model = new weikebuluo.Model.w_reply_model();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from w_reply where reply_id=@reply_id");

            SqlParameter[] parameters = {
					new SqlParameter("@reply_id", SqlDbType.Int,4)
			};

            parameters[0].Value = reply_id;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return CreateSQL.GetModel(model, ds);
            }
            else
            {
                return null;
            }
        }
        #endregion 得到一个对象实体

        #region 获取记录总数
        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM w_reply ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }

            object obj = DbHelperSQL.GetSingle(strSql.ToString());

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #endregion 获取记录总数

        #region 获得数据列表
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM w_reply");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion 获得数据列表

        #region 分页获取数据列表
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");

            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.reply_id desc");
            }

            strSql.Append(")AS Row, T.*  from w_reply T ");

            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }

            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion 分页获取数据列表

        #region 获得随机记录
        /// <summary>
        /// 获得随机记录
        /// </summary>
        public DataSet GetRandomList(string strWhere, string strNum)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + strNum + " * FROM (select * ");
            strSql.Append(" FROM w_reply ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            strSql.Append(") w_reply order by newid() ");

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion 获得随机记录
    }
}
